package de.uhilger.netzpult.server;

import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.uhilger.netzpult.shared.Document;
import de.uhilger.netzpult.shared.Folder;

public class NewsBox extends AbstractWebfilesServlet {

	private static final long serialVersionUID = -3476234696606899396L;
	
	public static final String ID = "id";
	public static final String OFFSET = "offset";
	public static final String COUNT = "count";

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		String folderIdStr = req.getParameter(ID);
		String offsetStr = req.getParameter(OFFSET);
		String countStr = req.getParameter(COUNT);
		Writer w = resp.getWriter();
		String pathInfo = req.getPathInfo();
		int depth = 0;
		if(pathInfo != null) {
			String[] elements = pathInfo.split("/");
			depth = elements.length;
		}
		String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		DocumentServer.header(w, "netzpult news", date, depth);
		w.write("<body id=\"newsBox\">");
		w.write("<div id=\"newsBox\">");
		if(folderIdStr != null) {
			try {
				int folderId = Integer.parseInt(folderIdStr);
				int offset = 0;
				int count = 5;
				if(offsetStr != null) {
					offset = Integer.parseInt(offsetStr);
				}
				if(countStr != null) {
					count = Integer.parseInt(countStr);
				}
				Folder folder = new Folder();
				folder.setId(folderId);
				String owner = ds.getFolderOwner(folder);
				List<Document> list = ds.getDocuments(owner, folder, true, " order by d.dc_timestamp desc", offset, count);
				Iterator<Document> i = list.iterator();
				SimpleDateFormat formatter = new SimpleDateFormat("dd. MMM. yyyy, HH:mm:ss Z z"); //.format(getCreationDateTime()
				while(i.hasNext()) {
					Document document = i.next();
					w.write("<p id=\"newsBoxDateTime\">");
					//w.write(document.getCreationTimeStr());
					w.write(formatter.format(document.getCreationDateTime()));
					w.write("</p><p id=\"newsBoxTitle\">");
					w.write("<a id=\"newsBoxLink\" href=\"/dok/");
					Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).finest("docId=" + document.getId());
					w.write(Integer.toString(document.getId()));
					w.write("\" target=\"_top\">");
					w.write(document.getTitle());
					w.write("</a>");
					w.write("</p>");
				}
				w.write("<p id=\"newsBoxLink\">");
				if(offset > 0) {
					int prevOffset = offset - count;
					if(prevOffset < 0) {
						prevOffset = 0;
					}
					w.write("<a id=\"newsBoxLink\" href=\"/content/newsBox?offset=");
					w.write(Integer.toString(prevOffset));
					w.write("&amp;count=");
					w.write(countStr);
					w.write("&amp;id=");
					w.write(folderIdStr);
					w.write("\">&lt;&nbsp;vorige Seite</a>&nbsp;|&nbsp;");
				}
				int nextOffset = offset + count;
				w.write("<a id=\"newsBoxLink\" href=\"/content/newsBox?offset=");
				w.write(Integer.toString(nextOffset));
				w.write("&amp;count=");
				w.write(countStr);
				w.write("&amp;id=");
				w.write(folderIdStr);
				w.write("\">n&auml;chste Seite&nbsp;&gt;</a></p>");
			} catch(NumberFormatException ex) {
				w.write("<p>Ung&uuml;ltiger Parameter f&uuml;r die Neuigkeiten-Liste:<br>");
				w.write("folderId: ");
				w.write(folderIdStr);
				w.write("<br>offset: ");
				w.write(offsetStr);
				w.write("<br>count: ");
				w.write(countStr);
				w.write("</p>");
			} catch (Exception e) {
				w.write("<p>Fehler beim Abruf der Neuigkeiten-Liste, ");
				w.write(e.getLocalizedMessage());
				w.write("</p>");
			}
		}
		w.write("</div></body></html>");
		w.flush();
		w.close();
	}


}
